<?php
class ContactController extends AppController {
	var $name = 'Contact';
	var $components = array('Menu','Init');
	var $uses = array();

	function beforeFilter() {
		$this->Init->startup($this);
		$this->Init->init();
		$this->Init->getKeyword();
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$_menus = $this->Menu->getMenu($this->params['controller'],$this->params['action']);
		for ($i = 1; $i <=6; $i++) {
			if ($_menus[$i-1] == 1) {
				$this->set('current'.$i,'class="current"');
			}
			else {
				$this->set('current'.$i,'');
			}
		}
		$this->set('activeNav','Contact');
	}

	function index() {
		$this->layout = 'inside';

		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);

		$this->loadModel('Content');

		$menus = $this->Content->find('first', array(
				'conditions' => array('Content.id' => 3)
		));
		$this->set('lang',$lang);
		$this->set('content',$menus);

		$this->set('captcha_image_url', Router::url(array('controller'=>'captcha', 'action'=>'securimage'))); //url for the captcha image

        $captcha_error_msg = __('The code you entered does not match', true);

		if (isset($this->data['Contact'])) {
			if( $this->captcha->check($this->data['Security']['code']) == false ) {
				//the code was incorrect - display an error message to user
                $this->set('error_captcha', $captcha_error_msg); //set error msg
                $this->render(); //reload page
			} else {
				$this->Email->from = $this->data['Contact']['Fullname'] . ' [' . $this->data['Contact']['Email'] . ']';
				$this->Email->to = MAIL_CONTACT;
				$this->Email->subject = $this->data['Contact']['Title'];
				$this->Email->template = 'contact';
				$this->Email->sendAs = 'html';
				$this->set('contact', $this->data['Contact']);
				$this->Email->send();
				$this->Session->setFlash(__('Thanks for your contact', true), 'default', array('class' => 'success'));
				$this->redirect(array('action'=>'index'));
			}
		}
		$this->render('index');
	}
}
?>
